package cn.swust.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import cn.swust.model.AdminBean;
import cn.swust.model.InformationBean;
import cn.swust.model.UserBean;
import cn.swust.service.AdminService;
import cn.swust.service.InformationService;
import cn.swust.service.UserService;
import cn.swust.service.serviceimpl.AdminServiceImpl;
import cn.swust.service.serviceimpl.InformationServiceImpl;
import cn.swust.service.serviceimpl.UserServiceImpl;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class LoginAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public String login(){
		Map<String, Object> session = ActionContext.getContext().getSession();
		String userName = ServletActionContext.getRequest().getParameter("username");
		String password = ServletActionContext.getRequest().getParameter("password");
		if(userName == null || password == null)
			return ERROR;
		AdminService admin = new AdminServiceImpl();
		AdminBean ad = admin.queryByName(userName);
		if(password.equals(ad.getPassword())){
			session.put("loginName", userName);
			return SUCCESS;
		}
		UserService user = new UserServiceImpl();
		UserBean userBean = user.queryByName(userName);
		String userPwd = userBean.getPassword();
		if(password.equals(userPwd)){
			session.put("loginName", userName);
			return SUCCESS;
		}
		return ERROR;
	}
	public String loginOut(){
		ActionContext.getContext().getSession().clear();
		ActionContext.getContext().getSession().put("message", "tips:");
		return SUCCESS;
	}
	
	//添加用户
	public String addUser(){
		HttpServletRequest request = ServletActionContext.getRequest();
		String userName = request.getParameter("user.name");
		UserService ser = new UserServiceImpl();
		UserBean userBean = ser.queryByName(userName);
		if(userBean.getId() > 0)
			return ERROR;
		userBean.setName(request.getParameter("user.name"));
		userBean.setPassword(request.getParameter("user.password"));
		userBean.setTel(request.getParameter("user.tel"));
		userBean.setGold(Float.valueOf(request.getParameter("user.goldPrice")));
		userBean.setLoveGold(Float.valueOf(request.getParameter("user.lovegold")));
		userBean.seteMail(request.getParameter("user.email"));
		userBean.setIdCard(request.getParameter("user.idcard"));
		userBean.setChName(request.getParameter("user.chName"));
		int classify = Integer.valueOf(request.getParameter("user.category"));
		userBean.setClassify(classify);
		int sex = Integer.valueOf(request.getParameter("user.sex"));
		userBean.setSex(sex);
		int judge = ser.addUser(userBean);
		if(judge > 0)
			return SUCCESS;
		return ERROR;
	}
	
	//添加信息：爱心通知，机构鸣谢，爱心文章心得，爱心屋简介，爱心屋新闻
	public String addInformation(){
		String infoTitle = ServletActionContext.getRequest().getParameter("infoTitle");
		String infoContent = ServletActionContext.getRequest().getParameter("infoContent");
		//0,爱心新闻，1爱心屋通知,2，鸣谢文章,3爱心心得文章
		String type = ServletActionContext.getRequest().getParameter("type");
		if(!type.matches("\\d+"))
			return ERROR;
		InformationService infoSer = new InformationServiceImpl();
		InformationBean infoBean = infoSer.queryByTitle(infoTitle);
		if(infoBean.getId() > 0)
			return ERROR;
		infoBean.setTitle(infoTitle);
		infoBean.setText(infoContent);
		infoBean.setType(Integer.valueOf(type));
		int result = infoSer.addInfor(infoBean);
		if(result > 0)
			return SUCCESS;
		return ERROR;
	}
}
